package com.example.springboot.controller;

import com.example.springboot.bean.Department;
import com.example.springboot.bean.Document;
import com.example.springboot.bean.Employee;
import com.example.springboot.entity.AjaxJson;
import com.example.springboot.entity.GlobalTags;
import com.example.springboot.service.DepartmentService;
import com.example.springboot.service.DocumentService;
import com.example.springboot.service.EmployeeService;
import com.example.springboot.util.file.FileUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tang on 2017/11/21.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    EmployeeService employeeService;

    @Autowired
    DocumentService documentService;

    @Autowired
    DepartmentService departmentService;


    @Value("${web.upload-path}")
    private String filePath;

    @Value("${web.img-path-prefix}")
    private String uploadImgPathPrefix;


    @RequestMapping("/findbyName")
    public ModelAndView findEmployee(String name) {
        ModelAndView mv = new ModelAndView();
        Employee employee = employeeService.findByName(name);
        mv.addObject("employee", employee);
        mv.setViewName("employee/employee");
        return mv;
    }


    @RequestMapping("/findByJdbc")
    public ModelAndView findByJdbc(String name) {
        ModelAndView mv = new ModelAndView();
        Employee employee = employeeService.findByName2(name);
        mv.addObject("employee", employee);
        mv.setViewName("employee/employee");
        return mv;
    }

    @RequestMapping(value = "list")
    public String employeeList(Map<String, Object> maps,
                               @RequestParam(defaultValue = "10") int pageSize,
                               @RequestParam(defaultValue = "1") int pageNo) {

        PageRequest pageRequest = new PageRequest(pageNo - 1, pageSize);
        Iterable<Employee> employees = employeeService.findAll(pageRequest);
        long count = employeeService.count();
        maps.put("pageNo", pageNo);
        maps.put("pageSize", pageSize);
        maps.put("count", count);
        maps.put("employees", employees);
        return "employee/employee-list";
    }


    @RequestMapping("add")
    public String addEmployeePage() {
        return "employee/add-employee";
    }

    @RequestMapping(value = "update/{id}")
    public String updateEmployeePage(@PathVariable int id, Map<String, Object> params) {
        params.put("employee", employeeService.findById(id));
        return "employee/update-employee";
    }


    @ResponseBody
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
    public AjaxJson deleteEmployee(@PathVariable int id) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            employeeService.delete(id);
            ajaxJson.setSuccess(true);
            ajaxJson.setMessage(GlobalTags.DELETE_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            ajaxJson.setSuccess(false);
            ajaxJson.setMessage(GlobalTags.DELETE_FAILURE);
        }
        return ajaxJson;
    }


    @ResponseBody
    @RequestMapping(value = "addEmployee", method = RequestMethod.POST)
    public AjaxJson addEmployee(Employee employee) {
        AjaxJson ajaxJson = new AjaxJson();

        try {
            employeeService.save(employee);
            ajaxJson.setSuccess(true);
            ajaxJson.setMessage(GlobalTags.SAVE_SUCCESS);

        } catch (Exception ex) {
            ex.printStackTrace();
            ajaxJson.setSuccess(false);
            ajaxJson.setMessage(GlobalTags.SAVE_FAILURE);
        }

        return ajaxJson;
    }

 /*   @ModelAttribute
    public void getEmployee(@PathVariable(required = false) Integer id, Model model) {
        Employee employee;
        if (id != null) {
            employee = employeeService.findById(id);
            model.addAttribute("employee", employee);
        }
    }*/


    @ResponseBody
    @RequestMapping(value = "updateEmpolyee", method = RequestMethod.PUT)
    public AjaxJson updateEmployee(Employee employee) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            //保存带id的employee 就是更新employee
            employeeService.save(employee);
            ajaxJson.setSuccess(true);
            ajaxJson.setMessage(GlobalTags.UPDATE_SUCCESS);

        } catch (Exception ex) {
            ex.printStackTrace();
            ajaxJson.setSuccess(false);
            ajaxJson.setMessage(GlobalTags.UPDATE_FAILURE);
        }
        return ajaxJson;
    }


    @ResponseBody
    @RequestMapping(value = "uploadEmployeeImg", method = RequestMethod.POST)
    public AjaxJson uploadEmployeeImg(@RequestParam(name = "file") MultipartFile file) {

        AjaxJson ajaxJson = new AjaxJson();
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            Document document = FileUploader.upload(file, filePath);
            documentService.save(document);
            params.put("document", document);
            params.put("uploadImgPathPrefix",uploadImgPathPrefix);
            ajaxJson.setAttributes(params);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ajaxJson;
    }


}
